<?php

class CmpNewsController extends BaseController
{

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new CmpNews;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['CmpNews']))
		{
			$model->attributes=$_POST['CmpNews'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->zcn_id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['CmpNews']))
		{
			$model->attributes=$_POST['CmpNews'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->zcn_id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * 单条批量删除
	 */
	public function actionDelete($id)
	{
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zcn_id in ($id)");
		
		if(CmpNews::model()->deleteAll($criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}	
	}



	/**
	 * 商消息审核
	 */
	public function actionAdmin()
	{
		$model=new CmpNews('search');
		$model->unsetAttributes();  // clear any default values
		$model->zcn_status = 1;//筛选待审核消息
		if(isset($_GET['CmpNews']))
			$model->attributes=$_GET['CmpNews'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=CmpNews::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='cmp-news-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	/**
	 *单条，批量设为审核通过
	 */
	public function actionCheckpass(){
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zcn_id in ($id)");
		//修改内容
		$set = array('zcn_status'=>'2');
		//执行操作
		if(CmpNews::model()->updateAll($set,$criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}
	
	/**
	 * 信息传送
	 */
	public function actionTransmit($id)
	{
		$cmpNew=$this->loadModel($id);
		
		$model = new News();
	
		if(isset($_POST['News']))
		{
			$model->attributes=$_POST['News'];
			//分类ID等于0为未选择
			if($model->znc_id==0){
				$model->znc_id = '';
			}
			if($model->save()){
				$this->redirect(array($this->pre_module_url.'/news/admin'));
			}
		}else{
			$model->zne_title = $cmpNew->zcn_title;
			$model->zm_id = $cmpNew->zm_id;
			$model->zne_content = $cmpNew->zcn_content;
			$model->zne_thumb = $cmpNew->zcn_pic;
			$model->zne_source = $cmpNew->company->zc_name;//来源为商家名称
		}
	
		$this->render('transmit',array(
				'model'=>$model,
		));
	}
}
